package com.zhuoshi.controller;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import com.zhuoshi.service.*;
import com.zhuoshi.serviceImpl.RedisOpera;

import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhuoshi.bean.WdApartmentLayout;
import com.zhuoshi.bean.WdHtProject;
import com.zhuoshi.bean.WdHtRoom;


@Controller
public class WdHtRoomController {

	@Autowired
	private WdRoomService WdRoomServiceImpl;
	
	//项目的service
	@Autowired
	private WdHtProjectService wdProjectService;
	
	
	 //操作redis的工具类
	 @Autowired
     private  RedisOpera redisOpera;//注入JedisPool
	
	/**
	 * 查询全部的房间
	 */   
	@RequestMapping("/room/list")
	public String list(Model model,WdHtRoom wdHtRoom,Integer currentPage,Integer rowsOfpage)
	{
		 //查询全部的项目
	    List<WdHtProject> projectList = wdProjectService.getAll(null);
	    model.addAttribute("projectList",projectList);
	    
	    	 // 引入分页查询，使用PageHelper分页功能
			// 在查询之前传入当前页，然后多少记录
	    if(currentPage!=null&&rowsOfpage!=null&&currentPage!=0&&rowsOfpage!=0)
	    {
	    	PageHelper.startPage(currentPage, rowsOfpage);
	    }
			// startPage后紧跟的这个查询就是分页查询
			List<WdHtRoom> list1 = WdRoomServiceImpl.selectAll(wdHtRoom);
			// 使用PageInfo包装查询结果，只需要将pageInfo交给页面就可以
			PageInfo<WdHtRoom> list = new PageInfo<WdHtRoom>(list1);
			model.addAttribute("list", list);
	    
		    return "room_list";
	}
	
	/**
	 * 查询一个房间的详细信息
	 */
	@RequestMapping("/room/selectOne")
	public String selectOne(Model model,WdHtRoom wdHtRoom)
	{
		List<WdHtRoom> list = WdRoomServiceImpl.selectAll(wdHtRoom);
		model.addAttribute("list", list);
		return "room_detail";
	}
	
	/**
	 * 删除一个房间的详细信息
	 */
	@RequestMapping("/room/deleteOne")
	public String deleteOne(Model model,String roomId)
	{
		WdRoomServiceImpl.deletOne(roomId);
		redisOpera.insertIntoRedis();
	
		return "redirect:/room/list.do";
	}
	
	/**
	 * 跳转到添加界面
	 */
	@RequestMapping("/room/toAdd")
	public String toAdd(Model model)
	{
		//查询全部的户型
		List<WdApartmentLayout> layOutList = WdRoomServiceImpl.selectAllLayOut();
	    model.addAttribute("layOutList", layOutList);	
	    //查询全部的项目
	    List<WdHtProject> projectList = wdProjectService.getAll(null);
	    model.addAttribute("projectList",projectList);
		return  "room_add";
	} 
	
	 /**
	 * 添加一个的功能
	 */
	 @RequestMapping("/room/add")
	 public String add(WdHtRoom wdHtRoom) {
	 //设置uuid
		 wdHtRoom.setRoomId(((UUID.randomUUID().toString())));
		 WdRoomServiceImpl.add(wdHtRoom);
		 
		 
			//更新 redis数据
		 redisOpera.insertIntoRedis();
	     return "redirect:/room/list.do";
	 }
	 
	 /**
	  * 跳转到批量生成jsp
	  */
	 
	 @RequestMapping("/room/createRoom")
	 public String toCreateRoom(Model model)
	 {
		//查询全部的户型
		List<WdApartmentLayout> layOutList = WdRoomServiceImpl.selectAllLayOut();
		model.addAttribute("layOutList", layOutList);	
		//查询全部的项目
		List<WdHtProject> projectList = wdProjectService.getAll(null);
		model.addAttribute("projectList",projectList);
	     return "createRoom";
	 }
	 
	 /**
	  * 跳转到批量生成jsp
	 * @throws IOException 
	  */
	 
	 @RequestMapping("/room/addRoomredis")
	 public void addRoomredis(@RequestBody String jsonData,HttpServletResponse resp) throws IOException
	 {
	     JSONArray array = JSONArray.fromObject(jsonData);
		 
		 List<WdHtRoom> list = (List<WdHtRoom>)JSONArray.toCollection(array, WdHtRoom.class);
		 
		 WdRoomServiceImpl.insertByList(list);
		 
		 
			//更新 redis数据
		 redisOpera.insertIntoRedis();
	  
         resp.getWriter().print("0");
	 }
	 
	 /**
	  *  查询redis中的所有的数据
	 * @throws IOException 
	  */
	 @RequestMapping("/room/listFromRedis")
	 public String listFromRedis(Model model)
	 {
		 List<WdHtRoom> listFromRedis = redisOpera.listFromRedis();
		 System.out.println(listFromRedis.size());
		 model.addAttribute("list",listFromRedis);
		 return "redis_list";
	 }
}
